package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.IntStream;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PointsRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PointsRuleSystemMapper;
import com.ruoyi.system.domain.PointsRuleSystem;
import com.ruoyi.system.service.IPointsRuleSystemService;
import org.springframework.util.CollectionUtils;

/**
 * 系统积分规则Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class PointsRuleSystemServiceImpl implements IPointsRuleSystemService
{
    @Autowired
    private PointsRuleSystemMapper pointsRuleSystemMapper;

    /**
     * 查询系统积分规则
     *
     * @param id 系统积分规则主键
     * @return 系统积分规则
     */
    @Override
    public PointsRuleSystem selectPointsRuleSystemById(Long id)
    {
        return pointsRuleSystemMapper.selectPointsRuleSystemById(id);
    }

    /**
     * 查询系统积分规则列表
     *
     * @param pointsRuleSystem 系统积分规则
     * @return 系统积分规则
     */
    @Override
    public List<PointsRuleSystem> selectPointsRuleSystemList(PointsRuleSystem pointsRuleSystem)
    {
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemMapper.selectPointsRuleSystemList(pointsRuleSystem);
        IntStream.range(0,pointsRuleSystems.size()).forEach(
                i->{
                    int index = i+1;
                    PointsRuleSystem pointsRuleSystemVo = pointsRuleSystems.get(i);
                    pointsRuleSystemVo.setOederNum(index);
                });
        return pointsRuleSystems;
    }

    /**
     * 新增系统积分规则
     *
     * @param pointsRuleSystem 系统积分规则
     * @return 结果
     */
    @Override
    public AjaxResult insertPointsRuleSystem(PointsRuleSystem pointsRuleSystem)
    {
        Long pointsRuleId = pointsRuleSystem.getPointsRuleId();
        String ruleType = pointsRuleSystem.getRuleType();
        String firstCondition = pointsRuleSystem.getFirstCondition();
        String cyclicalPattern = pointsRuleSystem.getCyclicalPattern();
        String modified = pointsRuleSystem.getModified();
        PointsRuleSystem ruleSystem = new PointsRuleSystem();
        ruleSystem.setPointsRuleId(pointsRuleId);
        ruleSystem.setRuleType(ruleType);
        ruleSystem.setFirstCondition(firstCondition);
        ruleSystem.setCyclicalPattern(cyclicalPattern);
        ruleSystem.setModified(modified);
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemMapper.selectPointsRuleSystemList(ruleSystem);
        if(!CollectionUtils.isEmpty(pointsRuleSystems)){
            return AjaxResult.error("不允许重复添加相同规则");
        }
        try {
            pointsRuleSystemMapper.insertPointsRuleSystem(pointsRuleSystem);
            return AjaxResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("添加失败");
        }
    }

    /**
     * 修改系统积分规则
     *
     * @param pointsRuleSystem 系统积分规则
     * @return 结果
     */
    @Override
    public AjaxResult updatePointsRuleSystem(PointsRuleSystem pointsRuleSystem)
    {
        String ruleType = pointsRuleSystem.getRuleType();
        String firstCondition = pointsRuleSystem.getFirstCondition();
        String cyclicalPattern = pointsRuleSystem.getCyclicalPattern();
        String modified = pointsRuleSystem.getModified();
        if(StringUtils.isNotBlank(ruleType)&&StringUtils.isNotBlank(firstCondition)&&StringUtils.isNotBlank(cyclicalPattern)&&StringUtils.isNotBlank(modified)){
            PointsRuleSystem ruleSystemById = pointsRuleSystemMapper.selectPointsRuleSystemById(pointsRuleSystem.getId());
            PointsRuleSystem ruleSystem = new PointsRuleSystem();
            Long pointsRuleId = ruleSystemById.getPointsRuleId();
            ruleSystem.setPointsRuleId(pointsRuleId);
            ruleSystem.setRuleType(ruleType);
            ruleSystem.setFirstCondition(firstCondition);
            ruleSystem.setCyclicalPattern(cyclicalPattern);
            ruleSystem.setModified(modified);
            List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemMapper.selectPointsRuleSystemList(ruleSystem);
            if(!CollectionUtils.isEmpty(pointsRuleSystems)){
                PointsRuleSystem ruleSystemVo = pointsRuleSystems.get(0);
                Long id = ruleSystemVo.getId();
                if(!id.equals(pointsRuleSystem.getId())){
                    return AjaxResult.error("不允许重复添加相同规则");
                }
            }
        }
        try {
            pointsRuleSystemMapper.updatePointsRuleSystem(pointsRuleSystem);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("修改失败");
        }
    }

    /**
     * 删除系统积分规则信息
     *
     * @param id 系统积分规则主键
     * @return 结果
     */
    @Override
    public int deletePointsRuleSystemById(Long id)
    {
        return pointsRuleSystemMapper.deletePointsRuleSystemById(id);
    }
}
